<!--视频监测数据-->
<template>
  <div class="videoMonitor-box" :style="{'font-size': fontSize}">
    <div class="flex-box">
      <template v-for="item in arryData">
        <div v-if="item.id === 'compass'" class="column-box" :style="{'min-width': columnWidth, 'max-width': columnWidth}">
          <slot />
        </div>
        <c-scroll v-else class="column-box" :style="{'min-width': columnWidth, 'max-width': columnWidth}" :auto-scroll="[3, 28, 1]">
          <div v-if="item.id === 'iot'" class="title">{{ monitorTime }}</div>
          <div class="arry-box">
            <div class="ll">
              <span v-for="(it, index) in item.list" :key="index">{{ it.name }}</span>
            </div>
            <div class="rr">
              <span v-for="(it, index) in item.list" :key="index">{{ it.value }}</span>
            </div>
          </div>
        </c-scroll>
      </template>
    </div>
  </div>
</template>
<script>
import { getAirNow, getSensorData, getWeatherNow } from '@/api/video/video';

export default {
  name: 'VideoMonitorData',
  props: {
    //视频数据
    videoData: {
      type: Object,
      default: {}
    },
    index: {
      type: Number,
      default: 0
    },
    //监测数据
    iotShow: {
      type: Boolean,
      default: false
    },
    //空气质量数据
    airShow: {
      type: Boolean,
      default: false
    },
    //气象数据
    weatherShow: {
      type: Boolean,
      default: false
    },
    compassShow: { //指北针
      default: false
    },
    screen: {} //分屏数据
  },
  watch: {
    iotShow(val) {
      this.dataHandle('iot', val);
    },
    airShow(val) {
      this.dataHandle('air', val);
    },
    weatherShow(val) {
      this.dataHandle('weather', val);
    },
    compassShow(val) {
      this.dataHandle('compass', val);
    }
  },
  data() {
    return {
      arryData: [],
      monitorTime: '',
      fontSize: '14px',
      columnWidth: '210px'
    };
  },
  beforeDestroy() {
    this.resizeObserver && this.resizeObserver.disconnect();
  },
  mounted() {
    this.resizeObserver = new ResizeObserver(() => {
      this.countFit(true);
    });
    this.resizeObserver.observe(this.$el);
  },
  methods: {
    /**
     * 计算适配
     */
    countFit(resize) {
      const { offsetWidth } = this.$el;
      if(offsetWidth <= 380) {
        this.fontSize = '14px';
        this.columnWidth = '210px';
        this.$el.style.setProperty('--compass-width', '80px');
      }else if(offsetWidth < 1920) {
        this.fontSize = '16px';
        this.columnWidth = '230px';
        this.$el.style.setProperty('--compass-width', '80px');
      }else {
        this.fontSize = '18px';
        this.columnWidth = '270px';
        this.$el.style.setProperty('--compass-width', '100px');
      }
      this.$nextTick(() => {
        const { scrollWidth } = this.$el;
        if(this.arryData.length > 1 && scrollWidth > offsetWidth) {
          let action = this.arryData[0];
          if(resize) {
            action = this.arryData[this.arryData.length - 1];
          }
          this.$emit(action.id + 'Close');
          this.countFit();
        }
      });
    },
    /**
     *数据处理
     */
    dataHandle(type, show) {
      this.arryData = this.arryData.filter(item => item.id !== type);
      if(show) {
        if(type === 'air') {
          this.getAir();
        }else if(type === 'weather') {
          this.getWeather();
        }else if(type === 'iot') {
          this.getIot();
        }else {
          this.arryData.push({
            id: type
          });
          this.countFit();
        }
      }
    },
    /**
     * 获取空气质量数据
     */
    getAir() {
      if (!this.videoData.sources[0].camera.longitude || !this.videoData.sources[0].camera.latitude) {
        const ts = '未获取到坐标地址';
        try{
          if(this.screen) {
            this.$parent.screenMessTs(this.screen, ts);
          }else {
            this.$parent.screenMessTs(ts);
          }
        }catch (e) {
          $message.w(ts);
        }
        return false;
      }
      let param = { 'adcode': this.videoData.sources[0].camera.longitude + ',' + this.videoData.sources[0].camera.latitude };
      getAirNow(param)
        .then((res) => {
          if(!this.airShow) {
            return false;
          }
          if (res.code != 200) {
            try{
              if(this.screen) {
                this.$parent.screenMessTs(this.screen, res.msg);
              }else {
                this.$parent.screenMessTs(res.msg);
              }
            }catch (e) {
              $message.a(res);
            }
            this.$emit('airClose');
          } else {
            let airData = res.data;
            let list = [];
            if (airData['category']) {
              list.push({ name: '空气质量指数：', value: airData['category'] + ' ' + airData['aqi'] });
            }
            if (airData['pm2p5']) {
              list.push({ name: 'PM2.5：', value: airData['pm2p5'] + '(μg/m³)' });
            }
            if (airData['pm10']) {
              list.push({ name: 'PM10：', value: airData['pm10'] + '(μg/m³)' });
            }
            if (airData['o3']) {
              list.push({ name: 'O₃：', value: airData['o3'] + '(μg/m³)' });
            }
            if (airData['co']) {
              list.push({ name: 'CO：', value: airData['co'] + '(μg/m³)' });
            }
            if (airData['so2']) {
              list.push({ name: 'SO₂：', value: airData['so2'] + '(μg/m³)' });
            }
            if (airData['no2']) {
              list.push({ name: 'NO₂：', value: airData['no2'] + '(μg/m³)' });
            }
            let data = {
              id: 'air',
              list: list
            };
            if(!this.arryData.some(item => item.id === 'air')) {
              this.arryData.push(data);
            }
            this.countFit();
          }
        });
    },
    /**
     * 获取气象数据
     */
    getWeather() {
      if (!this.videoData.sources[0].camera.longitude || !this.videoData.sources[0].camera.latitude) {
        const ts = '未获取到坐标地址';
        try{
          if(this.screen) {
            this.$parent.screenMessTs(this.screen, ts);
          }else {
            this.$parent.screenMessTs(ts);
          }
        }catch (e) {
          $message.a(ts);
        }
        return false;
      }
      let param = { 'adcode': this.videoData.sources[0].camera.longitude + ',' + this.videoData.sources[0].camera.latitude };
      getWeatherNow(param)
        .then((res) => {
          if(!this.weatherShow) {
            return false;
          }
          if (res.code != 200) {
            try{
              if(this.screen) {
                this.$parent.screenMessTs(this.screen, res.msg);
              }else {
                this.$parent.screenMessTs(res.msg);
              }
            }catch (e) {
              $message.a(res);
            }
            this.$emit('weatherClose');
          } else {
            let weatherData = res.data;
            let list = [];
            if (weatherData['wind360']) {
              list.push({ name: '风向：', value: weatherData['wind360'] + '°' });
            }
            if (weatherData['windSpeed']) {
              list.push({ name: '风速：', value: parseInt(weatherData['windSpeed'] / 3.6) + '(m/s)' });
            }
            if (weatherData['temp']) {
              list.push({ name: '气温：', value: weatherData['temp'] + '(℃)' });
            }
            if (weatherData['humidity']) {
              list.push({ name: '相对湿度：', value: weatherData['humidity'] + '(%RH)' });
            }
            if (weatherData['precip']) {
              list.push({ name: '降水量：', value: weatherData['precip'] + '(mm/min)' });
            }
            let data = {
              id: 'weather',
              list: list
            };
            if(!this.arryData.some(item => item.id === 'weather')) {
              this.arryData.push(data);
            }
            this.countFit();
          }
        });
    },
    /**
     * 获取物联监测数据
     */
    getIot() {
      let param = { 'deviceCode': this.videoData.sources[0].camera.deviceCode };
      getSensorData(param)
        .then((res) => {
          if(!this.iotShow) {
            return false;
          }
          if (res.code != 200) {
            try{
              if(this.screen) {
                this.$parent.screenMessTs(this.screen, res.msg);
              }else {
                this.$parent.screenMessTs(res.msg);
              }
            }catch (e) {
              $message.a(res);
            }
            this.$emit('iotClose');
          } else {
            let iotData = res.data;
            let list = [];
            for (let i = 0; i < iotData.length; i++) {
              iotData[i].attrValue = iotData[i].attrValue ? iotData[i].attrValue : '-';
              list.push({
                name: iotData[i].attrName.split('(')[0] + '：',
                value: iotData[i].attrValue + '(' + iotData[i].attrUnits + ')'
              });
            }
            if (iotData.length > 0) {
              this.monitorTime = iotData[0].monitorTime;
            }
            let data = {
              id: 'iot',
              list: list
            };
            if(!this.arryData.some(item => item.id === 'iot')) {
              this.arryData.push(data);
            }
            this.countFit();
          }
        });
    }
  }
};
</script>
<style scoped lang="scss">
.videoMonitor-box {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  padding: 12px 12px 12px 0;
  .flex-box {
    display: flex;
    height: 100%;
    width: 100%;
    color: #fff;
    line-height: 1;
    .column-box{
      width: auto;
      margin-left: 12px;
      overflow: hidden;
      .title{
        width: 100%;
        margin-bottom: 12px;
        font-weight: 600;
      }
      .arry-box{
        width: 100%;
        display: flex;
        height: auto;
        pointer-events: auto;
        .ll, .rr{
          overflow: hidden;
          span{
            display: block;
            margin-top: 12px;
            word-break: keep-all;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            font-weight: 600;
            &:first-child{
              margin-top: 0;
            }
          }
        }
        .ll{
          text-align: right;
        }
        .rr{
          flex: 1;
        }
      }
      .compass-div{
        position: relative !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: auto !important;
        display: flex;
        align-items: center;
        white-space: nowrap;
        padding: 0 !important;
        div{
          font-size: inherit !important;
          padding: 0 !important;
          width: auto !important;
          height: auto !important;
          .map-p{
            top: 0 !important;
          }
          p{
            font-size: inherit !important;
            color: #fff !important;
            text-shadow: none !important;
            padding: 0 0 0 12px !important;
            line-height: 1 !important;
            margin-top: 12px !important;
            font-weight: 600;
            &:first-child{
              margin-top: 0 !important;
            }
          }
        }
        .--left{
          width: var(--compass-width, 80px) !important;
          height: var(--compass-width, 80px) !important;
          position: relative !important;
          img, div{
            width: 100% !important;
            height: 100% !important;
          }
          .map-z, .map-t{
            clip: rect(0px, calc(var(--compass-width, 80px) / 2), var(--compass-width, 80px), 0px) !important;
          }
        }
        .--right{
          flex: 1;
        }
      }
    }
  }
}

</style>
<style lang="scss">
.t, .l2, .g, .ar-t, .ar-l2, .ar-g {
  .videoMonitor-box {
    .c-scroll .el-scrollbar .el-scrollbar__bar .el-scrollbar__thumb {
      background: transparent !important;
    }
  }
}
</style>
